package zwh.mall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import zwh.mall.dao.CategoryDao;
import zwh.mall.entity.Category;
import zwh.mall.service.CategoryService;

import java.util.List;

/**
 * @author 97506
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public Category findById(int id) {
        return categoryDao.getOne(id);
    }

    @Override
    public List<Category> findAll(int type) {
        return categoryDao.findByType(type);
    }

    /**
     * 按分类查询所有
     *
     * @param pageable
     * @return
     */
    @Override
    public Page<Category> findAll(int type, Pageable pageable) {
        return categoryDao.findByType(type, pageable);
    }

    @Override
    public List<Category> findAllExample(Example<Category> example) {
        return categoryDao.findAll(example);
    }

    @Override
    public void update(Category category) {
        categoryDao.save(category);
    }

    @Override
    public int create(Category category) {
        Category category1 = categoryDao.save(category);
        return category.getId();
    }

    @Override
    public void delById(int id) {
        categoryDao.delete(id);
    }

    /**
     * 通过一级分类id查找它所有的二级分类
     *
     * @param cid
     * @return
     */
    @Override
    public List<Category> findByParentId(int cid) {
        return categoryDao.findByParentId(cid);
    }
}
